package org.lotic.ldk.object;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.lotic.ldk.string.StringUtil;

/**
 * 对象工具类.
 * @author lotic
 *
 */
public class ObjectUtil {
	/**
	 * 日志对象.
	 */
	private static final Log log = LogFactory.getLog(ObjectUtil.class);
	/**
	 * 将pojo对象属性和值放入map集合中.key为属性名,value为属性值.
	 * @param pojo pojo对象
	 * @return Map
	 */
	public static Map<String, Object> pojoAttrToMap(Object pojo) {
		Map<String, Object> map = new HashMap<String, Object>();
		Class clazz = pojo.getClass();
		Field[] fields = clazz.getDeclaredFields();
		for (Field field : fields) {
			String str = field.getName();
			String getName = "get" + StringUtil.toUpperCaseFirst(str);
			Object value = null;
			try {
				 value = clazz.getMethod(getName, null).invoke(pojo, null);
			} catch (Exception e) {
				if (log.isErrorEnabled()) {
					log.error(getName + "方法调用出错!",e);
				}
				continue;
			}
			
			map.put(str, value);
					
		}
		return map;
	}
}
